package com.ruoyi.system.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.DeptFileArchived;
import com.ruoyi.system.domain.SysUserDevelop;
import com.ruoyi.system.service.IDeptFileArchivedService;
import com.ruoyi.system.service.ISysUserDevelopService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysUserStage2Mapper;
import com.ruoyi.system.domain.SysUserStage2;
import com.ruoyi.system.service.ISysUserStage2Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

/**
 * 入党积极分子阶段Service业务层处理
 *
 * @author ruoyi
 * @date 2024-04-15
 */
@Service
public class SysUserStage2ServiceImpl implements ISysUserStage2Service
{
    @Autowired
    private SysUserStage2Mapper sysUserStage2Mapper;
    @Autowired
    private ISysUserDevelopService sysUserDevelopService;
    @Autowired
    private IDeptFileArchivedService deptFileArchivedService;
    @Autowired
    private ISysUserService sysUserService;

    /**
     * 新增入党积极分子阶段
     *
     * @param sysUserStage2 入党积极分子阶段
     * @return 结果
     */
    @Override
    public int insertSysUserStage2(SysUserStage2 sysUserStage2)
    {
        sysUserStage2.setCreateTime(DateUtils.getNowDate());
        return sysUserStage2Mapper.insertSysUserStage2(sysUserStage2);
    }

    /**
     * 修改入党积极分子阶段
     *
     * @param sysUserStage2 入党积极分子阶段
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateSysUserStage2(SysUserStage2 sysUserStage2)
    {
        String createBy = sysUserStage2.getCreateBy();
        String userName = sysUserStage2.getDevelopUserName();
        SysUserStage2 sysUserStage = sysUserStage2Mapper.selectSysUserStage2ByDevelopId(sysUserStage2.getSysUserDevelopId());
        sysUserStage2.setUpdateTime(DateUtils.getNowDate());
        Date step3IdentifyingTime = sysUserStage2.getStep3IdentifyingTime();
        Date step4RecordTime = sysUserStage2.getStep4RecordTime();
        Long step5User1Id = sysUserStage2.getStep5User1Id();
        Date step6CheckFinishTime = sysUserStage2.getStep6CheckFinishTime();
        SysUserDevelop sysUserDevelop = new SysUserDevelop();
        sysUserDevelop.setId(sysUserStage.getSysUserDevelopId());
        //父归档文件
        DeptFileArchived deptFileArchived = deptFileArchivedService.selectDeptFileArchivedByAssociationId(String.valueOf(sysUserStage2.getSysUserDevelopId()));
        if(!ObjectUtils.isEmpty(step3IdentifyingTime)){
            sysUserStage2.setStep3Status("1");
            sysUserStage2.setStep3FinishTime(DateUtils.getNowDate());
            sysUserDevelop.setDevelopStage("推荐和确定入党积极分子阶段");
            sysUserDevelopService.updateSysUserDevelop(sysUserDevelop);
            //创建阶段3归档文件夹
            DeptFileArchived deptFileArchivedVo = new DeptFileArchived();
            deptFileArchivedVo.setDeptId(deptFileArchived.getDeptId());
            deptFileArchivedVo.setCreateBy(createBy);
            deptFileArchivedVo.setCreateName(userName);
            deptFileArchivedVo.setFileType("0");
            deptFileArchivedVo.setFileName("推荐和确定入党积极分子阶段阶段");
            deptFileArchivedVo.setParentId(deptFileArchived.getParentId());
            deptFileArchivedVo.setAssociationId(String.valueOf(sysUserStage2.getId()));
            Long archivedId = deptFileArchivedService.insertDeptFileArchived(deptFileArchivedVo);
            //创建阶段3文件
            DeptFileArchived deptFileArchivedVo2 = new DeptFileArchived();
            deptFileArchivedVo2.setDeptId(deptFileArchived.getDeptId());
            deptFileArchivedVo2.setCreateBy(createBy);
            deptFileArchivedVo2.setCreateName(userName);
            deptFileArchivedVo2.setFileType("1");
            deptFileArchivedVo2.setFileName("入党积极分子推荐表");
            deptFileArchivedVo2.setFileUrl(sysUserStage2.getStep3File1());
            deptFileArchivedVo2.setParentId(archivedId);
            deptFileArchivedVo2.setAssociationId(String.valueOf(sysUserStage2.getId()));
            deptFileArchivedService.insertDeptFileArchived(deptFileArchivedVo2);

            DeptFileArchived deptFileArchivedVo3 = new DeptFileArchived();
            deptFileArchivedVo3.setDeptId(deptFileArchived.getDeptId());
            deptFileArchivedVo3.setCreateBy(createBy);
            deptFileArchivedVo3.setCreateName(userName);
            deptFileArchivedVo3.setFileType("1");
            deptFileArchivedVo3.setFileName("确定入党积极分子会议记录");
            deptFileArchivedVo3.setFileUrl(sysUserStage2.getStep3File2());
            deptFileArchivedVo3.setParentId(archivedId);
            deptFileArchivedVo3.setAssociationId(String.valueOf(sysUserStage2.getId()));
            deptFileArchivedService.insertDeptFileArchived(deptFileArchivedVo3);

            DeptFileArchived deptFileArchivedVo4 = new DeptFileArchived();
            deptFileArchivedVo4.setDeptId(deptFileArchived.getDeptId());
            deptFileArchivedVo4.setCreateBy(createBy);
            deptFileArchivedVo4.setCreateName(userName);
            deptFileArchivedVo4.setFileType("1");
            deptFileArchivedVo4.setFileName("入党积极分子公示文件");
            deptFileArchivedVo4.setFileUrl(sysUserStage2.getStep3File3());
            deptFileArchivedVo4.setParentId(archivedId);
            deptFileArchivedVo4.setAssociationId(String.valueOf(sysUserStage2.getId()));
            deptFileArchivedService.insertDeptFileArchived(deptFileArchivedVo4);

            DeptFileArchived deptFileArchivedVo5 = new DeptFileArchived();
            deptFileArchivedVo5.setDeptId(deptFileArchived.getDeptId());
            deptFileArchivedVo5.setCreateBy(createBy);
            deptFileArchivedVo5.setCreateName(userName);
            deptFileArchivedVo5.setFileType("1");
            deptFileArchivedVo5.setFileName("入党积极分子公示结果确认表");
            deptFileArchivedVo5.setFileUrl(sysUserStage2.getStep3File4());
            deptFileArchivedVo5.setParentId(archivedId);
            deptFileArchivedVo5.setAssociationId(String.valueOf(sysUserStage2.getId()));
            deptFileArchivedService.insertDeptFileArchived(deptFileArchivedVo5);

        }else if(!ObjectUtils.isEmpty(step4RecordTime)){
            sysUserStage2.setStep4FinishTime(DateUtils.getNowDate());
            sysUserStage2.setStep4Status("1");
            sysUserDevelop.setDevelopStage("入党积极分子备案阶段");
            sysUserDevelopService.updateSysUserDevelop(sysUserDevelop);

            //创建阶段4归档文件夹
            DeptFileArchived deptFileArchivedVo = new DeptFileArchived();
            deptFileArchivedVo.setDeptId(deptFileArchived.getDeptId());
            deptFileArchivedVo.setCreateBy(createBy);
            deptFileArchivedVo.setCreateName(userName);
            deptFileArchivedVo.setFileType("0");
            deptFileArchivedVo.setFileName("入党积极分子备案阶段");
            deptFileArchivedVo.setParentId(deptFileArchived.getParentId());
            deptFileArchivedVo.setAssociationId(String.valueOf(sysUserStage2.getId()));
            Long archivedId = deptFileArchivedService.insertDeptFileArchived(deptFileArchivedVo);
            //创建阶段4文件
            DeptFileArchived deptFileArchivedVo2 = new DeptFileArchived();
            deptFileArchivedVo2.setDeptId(deptFileArchived.getDeptId());
            deptFileArchivedVo2.setCreateBy(createBy);
            deptFileArchivedVo2.setCreateName(userName);
            deptFileArchivedVo2.setFileType("1");
            deptFileArchivedVo2.setFileName("入党积极分子备案报告");
            deptFileArchivedVo2.setFileUrl(sysUserStage2.getStep4File1());
            deptFileArchivedVo2.setParentId(archivedId);
            deptFileArchivedVo2.setAssociationId(String.valueOf(sysUserStage2.getId()));
            deptFileArchivedService.insertDeptFileArchived(deptFileArchivedVo2);

            DeptFileArchived deptFileArchivedVo3 = new DeptFileArchived();
            deptFileArchivedVo3.setDeptId(deptFileArchived.getDeptId());
            deptFileArchivedVo3.setCreateBy(createBy);
            deptFileArchivedVo3.setCreateName(userName);
            deptFileArchivedVo3.setFileType("1");
            deptFileArchivedVo3.setFileName("入党积极分子情况等级表");
            deptFileArchivedVo3.setFileUrl(sysUserStage2.getStep4File2());
            deptFileArchivedVo3.setParentId(archivedId);
            deptFileArchivedVo3.setAssociationId(String.valueOf(sysUserStage2.getId()));
            deptFileArchivedService.insertDeptFileArchived(deptFileArchivedVo3);
        }else if(!ObjectUtils.isEmpty(step5User1Id)){
            sysUserStage2.setStep5Status("1");
            sysUserStage2.setStep5FinishTime(DateUtils.getNowDate());
            sysUserDevelop.setDevelopStage("指定培养联系人阶段");
            sysUserDevelopService.updateSysUserDevelop(sysUserDevelop);

            //创建阶段5归档文件夹
            DeptFileArchived deptFileArchivedVo = new DeptFileArchived();
            deptFileArchivedVo.setDeptId(deptFileArchived.getDeptId());
            deptFileArchivedVo.setCreateBy(createBy);
            deptFileArchivedVo.setCreateName(userName);
            deptFileArchivedVo.setFileType("0");
            deptFileArchivedVo.setFileName("指定培养联系人阶段");
            deptFileArchivedVo.setParentId(deptFileArchived.getParentId());
            deptFileArchivedVo.setAssociationId(String.valueOf(sysUserStage2.getId()));
            Long archivedId = deptFileArchivedService.insertDeptFileArchived(deptFileArchivedVo);
            //创建阶段5文件
            DeptFileArchived deptFileArchivedVo2 = new DeptFileArchived();
            deptFileArchivedVo2.setDeptId(deptFileArchived.getDeptId());
            deptFileArchivedVo2.setCreateBy(createBy);
            deptFileArchivedVo2.setCreateName(userName);
            deptFileArchivedVo2.setFileType("1");
            deptFileArchivedVo2.setFileName("相关资料");
            deptFileArchivedVo2.setFileUrl(sysUserStage2.getStep5File());
            deptFileArchivedVo2.setParentId(archivedId);
            deptFileArchivedVo2.setAssociationId(String.valueOf(sysUserStage2.getId()));
            deptFileArchivedService.insertDeptFileArchived(deptFileArchivedVo2);
        }else if(!ObjectUtils.isEmpty(step6CheckFinishTime)){
            sysUserStage2.setStep6Status("1");
            sysUserStage2.setStep6FinishTime(DateUtils.getNowDate());
            sysUserDevelop.setDevelopStage("培养教育考察阶段");
            sysUserDevelopService.updateSysUserDevelop(sysUserDevelop);

            //创建阶段6归档文件夹
            DeptFileArchived deptFileArchivedVo = new DeptFileArchived();
            deptFileArchivedVo.setDeptId(deptFileArchived.getDeptId());
            deptFileArchivedVo.setCreateBy(createBy);
            deptFileArchivedVo.setCreateName(userName);
            deptFileArchivedVo.setFileType("0");
            deptFileArchivedVo.setFileName("培养教育考察阶段");
            deptFileArchivedVo.setParentId(deptFileArchived.getParentId());
            deptFileArchivedVo.setAssociationId(String.valueOf(sysUserStage2.getId()));
            Long archivedId = deptFileArchivedService.insertDeptFileArchived(deptFileArchivedVo);
            //创建阶段6文件
            DeptFileArchived deptFileArchivedVo2 = new DeptFileArchived();
            deptFileArchivedVo2.setDeptId(deptFileArchived.getDeptId());
            deptFileArchivedVo2.setCreateBy(createBy);
            deptFileArchivedVo2.setCreateName(userName);
            deptFileArchivedVo2.setFileType("1");
            deptFileArchivedVo2.setFileName("相关资料");
            deptFileArchivedVo2.setFileUrl(sysUserStage2.getStep6File());
            deptFileArchivedVo2.setParentId(archivedId);
            deptFileArchivedVo2.setAssociationId(String.valueOf(sysUserStage2.getId()));
            deptFileArchivedService.insertDeptFileArchived(deptFileArchivedVo2);
        }
        sysUserStage2.setId(sysUserStage.getId());
        return sysUserStage2Mapper.updateSysUserStage2(sysUserStage2);
    }

    /**
     * 删除入党申请人阶段信息
     *
     * @param userDevelopId 入党申请人阶段主键
     * @return 结果
     */
    @Override
    public int deleteSysUserStage2ByUserDevelopId(Long userDevelopId){
        return sysUserStage2Mapper.deleteSysUserStage2ByUserDevelopId(userDevelopId);
    }

    /**
     * 查询入党申请人阶段
     *
     * @param userDevelopId 入党申请人阶段主键
     * @return 入党申请人阶段
     */
    @Override
    public SysUserStage2 selectSysUserStage2ByDevelopId(Long userDevelopId){
        return sysUserStage2Mapper.selectSysUserStage2ByDevelopId(userDevelopId);
    }
}
